/*
 *  Jiki - a small lightweight wiki engine.
 *
 *  Copyright 2008 Peter Halbmayer <phalbmayer@gmail.com
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */


package at.pmsa.jiki.util;

import android.app.Activity;
import android.util.Log;

/** class DBMan has to achieve similar tasks than
 * the IOMan class but uses the Android built
 * in database this time
 * @author pezi
 *
 */

public class DBMan implements DataMan{
	
	DBHelper dbHelper;
	
	public DBMan(Activity a){
		dbHelper = new DBHelper(a);
	}
	
	public void close(){
		dbHelper.close();
	}
	
//	public void putFile(String fileName, String contents){
//		Log.v("DBMan", "Putting file: " + fileName);
//		int idx = rowList.size();
//		Row r = new Row(idx + 1, fileName, contents);
//		rowList.put(fileName, r);
//	}
//	
//	public String getFile(String fileName){
//		Log.v("DBMan", "Getting file: " + fileName);
//		Row r;
//		if((r = rowList.get(fileName)) == null)
//			return null;
//		else
//			return r.body;
//	}
//	
//	public boolean objExists(String fileName){
//		if(rowList.containsKey(fileName))
//			return true;
//		else
//			return false;
//	}

	public void writeFile(String fileName, String contents){
		Log.v("DBMan", "Writing file: " + fileName);
//		if(dbHelper.fetchRow(fileName) == null)
//			dbHelper.createRow(fileName, contents);
//		else
		//update row confines if row has to be created or updated
			dbHelper.updateRow(fileName, contents);
	}

	public String readFile(String fileName){
		Log.v("DBMan", "Reading file: " + fileName);
		Row r;
		if((r = dbHelper.fetchRow(fileName)) != null){
			return r.body;
		}
		else
			return null;
	}
	
	public String[] getAllFileNames(){
		return dbHelper.fetchRowNames();
	}
	
	public boolean fileExists(String fileName){
		if(dbHelper.fetchRow(fileName) != null)
			return true;
		else
			return false;
	}

//    public boolean writeBack(Hashtable<String, Row> rowList){
//    	try{
//    		Enumeration<Row> rowEnum = rowList.elements();
//    		while(rowEnum.hasMoreElements()){
//    			Row r = (Row)rowEnum.nextElement();
//    			if(fileExists(r.title))
//    				dbHelper.updateRow(r.rowid, r.title, r.body);
//    			else
//    				dbHelper.createRow(r.rowid, r.title, r.body);
//    		}
//    		return true;
//    	}
//    	catch(Exception e){
//    		Log.e("DBHelper", "writing back of WIKI content to storage failed");
//    		return false;
//    	}
//    }
}
